import os
import sys

from common.readCofig import Environ
import yaml
import os
import re


class ReadYaml():
    def __init__(self,filename):

        self.filepath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + Environ+"/"+filename)#拼接定位到data文件夹


    def get_yaml_data(self):#读取
        #print(self.filepath)
        with open(self.filepath, "r", encoding="utf-8")as f:
            # 调用load方法加载文件流
            return yaml.load(f,Loader=yaml.FullLoader)


    def write_yaml_data(self,data):#写入
        from ruamel import yaml
        #print(self.filepath)
        # 写入到yaml文件
        with open(self.filepath, "w", encoding="utf-8") as f:
            yaml.dump(data, f, Dumper=yaml.RoundTripDumper)


    def get_read_vcf(self):
        file = open(self.filepath, 'r', encoding='utf-8')
        contacts = []
        phone = []
        for line in file:
            name = re.findall('FN:(.*)', line)
            nm = ''.join(name)
            if len(nm) == 0:
                continue
            data = {'name': nm.strip()}
            for lin in file:
                tel = re.findall('TEL;CELL:(.*)', lin)
                tel = ''.join(tel)
                if len(tel) == 0:
                    continue
                tel = tel.strip()
                tel = ''.join(e for e in tel if e.isalnum())
                data['phone'] = tel
                break
            contacts.append(data)
        return contacts


if __name__ == '__main__':

    data = ReadYaml("config01.yaml").get_yaml_data()  # 读取数据

    print(data)


